﻿Imports System.Data.SqlClient
Public Class BodegaDAO
    Inherits Conexion
    Dim cmd As New SqlCommand
    'Funciones
    Public Function mostrar() As DataTable
        Try
            conectar()
            cmd = New SqlCommand("bodegas_mostrar")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            Dim da As SqlDataReader = cmd.ExecuteReader
            Dim dt As New DataTable
            dt.Load(da)
            If dt.Rows.Count > 0 Then
                Return dt
            Else

                Return Nothing
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                          MessageBoxIcon.Warning)
            Return Nothing
        Finally
            desconectar()
        End Try
    End Function
    Public Function mostrar_movimientos(ByVal reg As Bodega) As DataTable
        Try
            conectar()
            cmd = New SqlCommand("bodegas_mostrar_mov")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            cmd.Parameters.AddWithValue("@id", reg.pIdTBodega)
            Dim da As SqlDataReader = cmd.ExecuteReader
            Dim dt As New DataTable
            dt.Load(da)
            If dt.Rows.Count > 0 Then
                Return dt
            Else

                Return Nothing
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                          MessageBoxIcon.Warning)
            Return Nothing
        Finally
            desconectar()
        End Try
    End Function
    Public Function mostrarTipoBodegas() As DataTable
        Try
            conectar()
            cmd = New SqlCommand("TBodegas_mostrar")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            Dim da As SqlDataReader = cmd.ExecuteReader
            Dim dt As New DataTable
            dt.Load(da)
            If dt.Rows.Count > 0 Then
                Return dt
            Else

                Return Nothing
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                          MessageBoxIcon.Warning)
            Return Nothing
        Finally
            desconectar()
        End Try
    End Function

    Public Function agregar(ByVal reg As Bodega) As Boolean
        Try
            conectar()
            cmd = New SqlCommand("bodegas_agregar")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            'Parametros
            cmd.Parameters.AddWithValue("@des", reg.pDescrip)
            cmd.Parameters.AddWithValue("@idTBodega", reg.pIdTBodega)
            cmd.Parameters.AddWithValue("@accion", SqlDbType.Bit)
            cmd.Parameters("@accion").Direction = ParameterDirection.Output
            cmd.ExecuteNonQuery()
            Return cmd.Parameters("@accion").Value
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                        MessageBoxIcon.Warning)
            Return False
        Finally
            desconectar()
        End Try
    End Function

    Public Function actualizar(ByVal reg As Bodega) As Boolean
        Try
            conectar()
            cmd = New SqlCommand("bodegas_actualizar")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            'Parametros
            cmd.Parameters.AddWithValue("@idBod", reg.pIdBodega)
            cmd.Parameters.AddWithValue("@des", reg.pDescrip)
            cmd.Parameters.AddWithValue("@idTBodega", reg.pIdTBodega)
            cmd.Parameters.AddWithValue("@accion", SqlDbType.Bit)
            cmd.Parameters("@accion").Direction = ParameterDirection.Output
            cmd.ExecuteNonQuery()
            Return cmd.Parameters("@accion").Value
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                        MessageBoxIcon.Warning)
            Return False
        Finally
            desconectar()
        End Try
    End Function

    Public Function eliminar(ByVal reg As Bodega) As Boolean
        Try
            conectar()
            cmd = New SqlCommand("bodegas_eliminar")
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = cnn
            'Parametros
            cmd.Parameters.AddWithValue("@idBod", reg.pIdBodega)
            cmd.Parameters.AddWithValue("@des", reg.pDescrip)
            cmd.Parameters.AddWithValue("@idTBodega", reg.pIdTBodega)
            cmd.Parameters.AddWithValue("@accion", SqlDbType.Bit)
            cmd.Parameters("@accion").Direction = ParameterDirection.Output
            cmd.ExecuteNonQuery()
            Return cmd.Parameters("@accion").Value
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Advertencia", MessageBoxButtons.OK,
                        MessageBoxIcon.Warning)
            Return False
        Finally
            desconectar()
        End Try
    End Function
End Class
